//
//  CircleFormAction.cpp
//  fingeradvanture
//
//  Created by shuaibincheng on 16/5/30.
//
//

#include "CircleFormAction.hpp"
#include "TroopForm.hpp"
void CircleFormAction::train(const Vector<cocos2d::Node *> &monsters)
{
    if(!form)
        return;
    //计算向左侧移动的距离
    auto leftTranslation = form->marginLeft();
    //计算向右侧移动的距离
    auto rightTranslation = form->marginRight();
    
    int end = (monsters.size() % 2)?(monsters.size() - 1):monsters.size();
    for(int i = 0; i < end; i++)
    {
        auto leftMoveBy = MoveBy::create(1.0f, Vec2(-leftTranslation, 0.0f));
        auto rightMoveBy = MoveBy::create(1.0f, Vec2(rightTranslation, 0.0f));
        
        auto sequence = Sequence::create(leftMoveBy, leftMoveBy->reverse(), rightMoveBy, rightMoveBy->reverse(), NULL);
        auto repeatTrans = Repeat::create(sequence, 10);
        
        auto rotation = RotateBy::create(6.0f, 360.0f);
        auto repeatRotation = Repeat::create(rotation, 10);
        
        auto downMoveBy = MoveBy::create(15.0f, Vec2(0.0f, -form->getVisibleSize().height
                                                                 - form->getTroopSize().height - 20));
        auto spawn = Spawn::create(repeatTrans, repeatRotation, downMoveBy, NULL);
        monsters.at(i)->runAction(spawn);
        
//        auto x = monsters.at(i)->getPositionX();
//        auto y = monsters.at(i)->getPositionY();
//        monsters.at(i)->setPosition(x, form->getVisibleSize().height);
    }
    if(end < monsters.size())
    {
        auto center = monsters.at(end);
        
//        auto x = center->getPositionX();
//        auto y = center->getPositionY();
//        center->setPosition(x, form->getVisibleSize().height);
        
        auto leftMoveBy = MoveBy::create(1.0f, Vec2(-leftTranslation, 0.0f));
        auto rightMoveBy = MoveBy::create(1.0f, Vec2(rightTranslation, 0.0f));
        
        auto sequence = Sequence::create(leftMoveBy, leftMoveBy->reverse(), rightMoveBy, rightMoveBy->reverse(), NULL);
        auto repeatTrans = Repeat::create(sequence, 10);
        
        auto downMoveBy = MoveBy::create(15.0f, Vec2(0.0f, -form->getVisibleSize().height
                                                     - form->getTroopSize().height - 20));
        auto spawn = Spawn::create(repeatTrans, downMoveBy, NULL);
        center->runAction(spawn);
    }
}